package top.imyzt.generator;
import org.junit.Test;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 * 代码生成器
 * </p>
 * 
 * <code>
 * 简单代码生成器<br/>
 * https://gitee.com/baomidou/mybatisplus-spring-boot.git
 * </p>
 * <p>
 * 复杂代码生成器
 * https://gitee.com/baomidou/mybatisplus-spring-boot/blob/dev/src/test/java/com/baomidou/springboot/test/generator/GeneratorServiceEntity.java
 * </p>
 */
public class MpGenerator {

	//生成时打开
//	@Test
	public void generateCode(){
		//生成的包名
		String packageName = "top.imyzt";
		//需要生成的表名
		generateByTables(packageName, "file");
	}

	private void generateByTables(String packageName, String... tableNames){
		GlobalConfig config = new GlobalConfig();
		String dbUrl = "jdbc:mysql://localhost:3306/mountain";
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL)
		.setUrl(dbUrl)
		.setUsername("root")
		.setPassword("root")
		.setDriverName("com.mysql.jdbc.Driver");
		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig
		.setCapitalMode(true)
		.setEntityLombokModel(false)
		.setDbColumnUnderline(true)
		.setNaming(NamingStrategy.underline_to_camel)
		.setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
		config.setActiveRecord(false)
		.setAuthor("imyzt")//作者
		.setOutputDir("src\\main\\java")//生成路径,尽量不要改动
		.setFileOverride(true);
		new AutoGenerator().setGlobalConfig(config)
		.setDataSource(dataSourceConfig)
		.setStrategy(strategyConfig)
		.setPackageInfo(
				new PackageConfig()
				.setParent(packageName)
				.setController("controller")
				.setEntity("entity")
				).execute();
	}
}